﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_ITM_BOM : RibbonForm
    {
        public FND_ITM_BOM()
        {   
            InitializeComponent();
        }

        public string ItemGroup { get; set; }
        public string SelectedItemCode { get; set; }
        public string SelectedItemDescription { get; set; }
        public string SelectedItemUnitMeasure { get; set; }

        public string ConnectionString { get; set; }

        void BindGrid()
        {
            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = new DataTable();
            if (cboSearch.SelectedIndex == 0)
            {
                _table = oManager.BrowseInventoryItemByName(txtSearch.Text, cboItemGroup.Text);
            }
            else
            {
                _table = oManager.BrowseInventoryItemByCode(txtSearch.Text, cboItemGroup.Text);
            }
            oManager.Close();

            gridControl1.DataSource = _table;
            gridControl1.Refresh();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BindGrid();
        }

        private void FND_ITM_BOM_Load(object sender, EventArgs e)
        {
            cboItemGroup.SelectedIndex = 0;
            cboSearch.SelectedIndex = 1;
            cboItemGroup.Enabled = false;
            cboItemGroup.Text = this.ItemGroup;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {

            int iRowNum = gridView1.FocusedRowHandle;

            if (iRowNum >= 0)
            {
                this.SelectedItemCode = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["ItemCode"]).ToString();
                this.SelectedItemDescription = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["Description"]).ToString();
                this.SelectedItemUnitMeasure = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["InvtyUntMsr"]).ToString();
                this.Close();
            }
            else
            {
                MessageBox.Show("There was no selected item, please select item before clicking button choose!!!", "Choose", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }

            if (e.KeyCode == Keys.Tab)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindGrid();
            }
        }

        
    }
}
